package com.global.college.base.baselist;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.global.college.R;
import com.global.college.widget.HnLoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 项目名称：恒实直播电商
 * 类描述：列表的公共类
 */
public abstract class CommListFragment extends HnViewPagerBaseFragment implements OnRefreshListener, OnLoadmoreListener, HnLoadingLayout.OnReloadListener {

    public CommRecyclerAdapter mAdapter;
    @BindView(R.id.mRecycler)
    RecyclerView mRecycler;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.mHnLoadingLayout)
    HnLoadingLayout mHnLoadingLayout;
    Unbinder unbinder;
    private boolean isGrid = false;

    private OnEmptyClickListener listener;


    @Override
    public int getContentViewId() {
        return R.layout.comm_list;
    }


    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadmoreListener(this);
        mHnLoadingLayout.setOnReloadListener(this);
        getData();
        //适配器
        mAdapter = setAdapter();
        initRecycler();

    }


    /**
     * 加载数据
     */
    protected abstract void getData();

    /**
     * 初始化适配器
     *
     * @return
     */
    protected abstract CommRecyclerAdapter setAdapter();

    public void setGridManager(boolean isGrid) {
        this.isGrid = isGrid;
    }


    /**
     * 方法描述：Recycler列表初始化
     */
    private void initRecycler() {
        if (isGrid) {
            mRecycler.setLayoutManager(new GridLayoutManager(mActivity, 2));
            mRecycler.addItemDecoration(new HnRecyclerGridDecoration(8));
        } else {
            mRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        }

        mRecycler.setHasFixedSize(true);
        mRecycler.setAdapter(mAdapter);
    }

    protected void setEmpty(String content, int res) {
        if (mAdapter == null) return;
        if (mAdapter.getItemCount() < 1) {
            mHnLoadingLayout.setStatus(HnLoadingLayout.Empty);
            mHnLoadingLayout.setEmptyText(content).setEmptyImage(res);
        } else {
            mHnLoadingLayout.setStatus(HnLoadingLayout.Success);
        }
    }

    protected void setEmptyJump() {
        if (mAdapter == null) return;
        if (mAdapter.getItemCount() < 1) {
            mHnLoadingLayout.setEmptyJupmTextVisable();
        } else {
            mHnLoadingLayout.setEmptyJupmTextGong();
        }
    }

    /**
     * 停下下拉刷新
     */
    protected void refreshFinish() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh(500);
            smartRefreshLayout.resetNoMoreData();
        }
    }

    /**
     * 停止加载更多
     */
    protected void finishLoadmore() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadmore(500);
        }
    }

    /**
     * 停止加载，没有更多数据
     */
    protected void finishLoadmoreWithNoMoreData() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadmoreWithNoMoreData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("CommListFragment");

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("CommListFragment");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onReload(View v) {
        switch (v.getId()) {
            case R.id.tv_empty_jump:
                if (listener != null) {
                    listener.onViewClick(1);
                }
                break;
        }

    }

    /**
     * 设置ReloadButton的监听器
     *
     * @param listener
     * @return
     */
    public CommListFragment setOnEmptyClickListener(OnEmptyClickListener listener) {

        this.listener = listener;
        return this;
    }

    public interface OnEmptyClickListener {
        void onViewClick(int tag);
    }
}
